package com.pet.server.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pet.server.domain.entity.Employee;
import com.pet.server.service.IEmployeeService;
import com.pet.server.utils.ResultUtil;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "EmployeeServlet", urlPatterns = "/employee")
public class EmployeeServlet extends HttpServlet {
  @Resource private IEmployeeService service;

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String method = request.getParameter("method");
    if (method.equalsIgnoreCase("show")) {
      getAll(request, response);
    }
  }

  /**
   * 查询门店员工
   *
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */
  protected void getAll(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    int shopId = Integer.parseInt(request.getParameter("shopId"));
    List<Employee> list = service.getAll(shopId);
    ResultUtil<List> resultUtil = null;
    if (list != null) {
      resultUtil = new ResultUtil<>(list);
    } else {
      resultUtil = new ResultUtil<>("服务器异常");
    }
    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(resultUtil);
    response.getWriter().write(json);
  }
}
